module bcd8 (input [7:0] binary, output reg [3:0] ones, tens);
  
  reg [3:0] hundreds;
  integer i;

  always @(binary) begin

    hundreds = 4'd0;
    tens = 4'd0;
    ones = 4'd0;


    for (i=0; i<8; i=i+1) begin
      if (hundreds >= 4'd5) hundreds = hundreds +4'd3;
      if (tens >= 4'd5) tens = tens +4'd3;
      if (ones >=4'd5) ones = ones+4'd3;

      //shift
      hundreds = hundreds << 1;
      hundreds[0] = tens[3];
      tens = tens << 1;
      tens[0] = ones[3];
      ones = ones << 1;
      ones[0] = binary[3];
    end
  end
endmodule

module bcd12 (input [11:0] binary, output reg [3:0] ones, tens, hundreds, thousands);

  integer i;

  always @(binary) begin

    thousands = 4'd0;
    hundreds = 4'd0;
    tens = 4'd0;
    ones = 4'd0;


    for (i=0; i<11; i=i+1) begin
      if (thousands >= 4'd5) thousands = thousands +4'd3;
      if (hundreds >= 4'd5) hundreds = hundreds +4'd3;
      if (tens >= 4'd5) tens = tens +4'd3;
      if (ones >=4'd5) ones = ones+4'd3;

      //shift
      thousands = thousands << 1;
      thousands [0] = hundreds[3];
      hundreds = hundreds << 1;
      hundreds[0] = tens[3];
      tens = tens << 1;
      tens[0] = ones[3];
      ones = ones << 1;
      ones[0] = binary[3];
    end
  end
endmodule

